// 分享的用户
const app = getApp();
import { PACKAGE_OWNER_PAGE } from '../../../constants/route';
import { ActivityMixins, PageCommonFuncs, PageListMixins, PageOssMixins, ShareImageMixins } from '../../../behaviors/page';
import { tipError, showLoading, hideLoading, navTo } from '../../../common/env';
import { getSharelUserInfo } from '../../../api/common';
import { getUserEndorsementActivityList } from '../../../api/usr';
import { getUserId } from '../../../biztools/wex';
let userId;

Page({
    mixins: [ActivityMixins, PageCommonFuncs, PageListMixins, PageOssMixins, ShareImageMixins],
    data: {
        shareUserInfo: {},
        activityList: [],
        canManage: false, // 能否管理
    },
    onLoad(options) {
        userId = options.userId;
        console.log('ShareUser => 分享的用户id为：', userId);
        if (!userId) {
            tipError('会员不存在');
            setTimeout(() => {
                this.gotoHomePage();
            }, 2000);
            return;
        }
        if (getUserId() === userId && options.owner) {
            this.setData({
                canManage: true,
            });
        }
    },
    onShow: async function () {
        if (!app.globalData.isReadyAuth) {
            const _ = this;
            const originFunc = app.userIdentifyCallback;
            app.userIdentifyCallback = function (...args) {
                _.onShow(...args);
                return originFunc && originFunc(...args);
            }
            return;
        } else {
            const shareUserInfo = await getSharelUserInfo(userId);
            console.log('ShareUser => 分享的用户信息：', shareUserInfo);
            this.setData({
                shareUserInfo,
            });
        }
    },
    getListData: async function (isReplace=false) {
        showLoading('加载中...');
        const { pageNo, pageSize, pageTime, activityList } = this.data;
        const list = await getUserEndorsementActivityList(userId, {
            pageNo,
            pageSize,
            ts: pageTime,
        });
        this.setData({
            activityList: isReplace ? list : [...activityList, ...list],
        });
        hideLoading();
    },
    editUserSettingAction: function() {
        if (!this.data.canManage) return;
        navTo(PACKAGE_OWNER_PAGE.ownerSetting);
    },
})